package token

import (
	"bytes"
)

var ext = []byte("#EXT")

type Line []byte

func (l Line) String() string {
	return string(l)
}

func (l Line) Kind() Kind {
	if len(l) == 0 {
		return KindUnknown
	}
	if l[0] != '#' {
		return KindURI
	}
	if !bytes.HasPrefix(l, ext) {
		return KindComment
	}
	return KindTag
}

func (l Line) URI() string {
	if len(l) == 0 {
		return ""
	}
	return string(l)
}

func (l Line) Comment() string {
	if len(l) == 0 {
		return ""
	}
	return string(bytes.TrimSpace(l[1:]))
}

var colon = []byte(":")

func (l Line) Tag() (string, TagValue) {
	if len(l) == 0 {
		return "", nil
	}
	name, value, _ := bytes.Cut(l[1:], colon)
	return string(name), value
}

var hashtag = []byte("#")

func NewLineComment(content string) Line {
	return append(hashtag, content...)
}

func NewLineURI(uri string) Line {
	return Line(uri)
}

func NewLineTag(name string, value TagValue) Line {
	return bytes.Join([][]byte{hashtag, []byte(name), colon, value}, []byte(""))
}
